                        

 %% First, need 3 month flows
 %  Divide sample period into non-overlapping quarters.
 %  Note - this assumes an integer # of quarters are simulated.
qts  = months/3; 
EmpMatmo  = [Emp0, EmpEndofmo];
 %
EmpGrowth3mo  = zeros(NumFirms, qts );
LayoffRate3mo = zeros(NumFirms, qts );
HireRate3mo   = zeros(NumFirms, qts );
QuitRate3mo   = zeros(NumFirms, qts );
for j = 1:qts            
    j1= 3*j - 2;
    j2= 3*j;    
    EmpGrowth3mo(:,j)  = (EmpMatmo(:, j2+1) - EmpMatmo(:,j1)) ./ (0.5*(EmpMatmo(:, j2+1) + EmpMatmo(:,j1)));
    LayoffRate3mo(:,j) = sum(LayoffTot1mo(:, j1:j2), 2) ./ (0.5*(EmpMatmo(:, j2+1) + EmpMatmo(:,j1)));
    HireRate3mo(:,j) = sum(HireTot1mo(:, j1:j2), 2) ./ (0.5*(EmpMatmo(:, j2+1) + EmpMatmo(:,j1)));
    QuitRate3mo(:,j) = sum(QuitTot1mo(:, j1:j2), 2) ./ (0.5*(EmpMatmo(:, j2+1) + EmpMatmo(:,j1)));
end
 %
 % Reshape Flows:
EmpGrowthVec  = reshape( EmpGrowth3mo,  NumFirms*qts, 1);
LayoffRateVec = reshape( LayoffRate3mo, NumFirms*qts, 1);
HireRateVec   = reshape( HireRate3mo, NumFirms*qts, 1);
QuitRateVec   = reshape( QuitRate3mo, NumFirms*qts, 1);
SepRateVec    = QuitRateVec + LayoffRateVec;
 %
 % Avg Employment over quarter (will need for weighting).
EmpMatqt   = [Emp0, EmpEndofmo(:,(3:3:months))];   % select last month of each qt
EmpMat2    = EmpMatqt(:, 2 : end  );
EmpMat1    = EmpMatqt(:, 1 : end-1);
AvgEmpMat  = .5*(EmpMat1+EmpMat2);  % avg. of final months of adjacent quarters.
AvgEmpVec  = reshape( AvgEmpMat, NumFirms*qts, 1);
TotAvgEmp  = sum(AvgEmpVec);
 %
 %
 %% Make Hockey Stick 
binsr = [.01; .02; .04;  .06;  .08;  .1;  .125; .15;  .20;  .25;  .30;  .35;  .40;  .45;  .50;  .6;  .7;  .8;  .9;   1;   1.1; ...
         1.2;  1.3;  1.4;  1.5;  1.6;  1.7;  1.8;  1.9; 2.0];
binsl = [0;   .01; .02;  .04;  .06;  .08; .1;   .125; .15;  .20;  .25;  .30;  .35;  .40;  .45;  .5;  .6;  .7;  .8;  .9;   1.0; ...
         1.1;  1.2;  1.3;  1.4;  1.5;  1.6;  1.7;  1.8; 1.9];   % narrow bins near zero.
binsR = [sort(-1*binsl); binsr];
binsL = [sort(-1*binsr); binsl];
midpt = .5*(binsL+binsR);
bins  = length(binsR);
 %
binLayoffRate = zeros(bins,1);
binHireRate= zeros(bins,1);
binQuitRate= zeros(bins,1);
binSepRate= zeros(bins,1);
for i = 1:bins
    pos = find( binsL(i) <EmpGrowthVec & EmpGrowthVec <binsR(i));
    if isempty(pos) ==0
        binEmpWeight     = AvgEmpVec(pos)' /sum(AvgEmpVec(pos));
        binLayoffRate(i) = binEmpWeight*LayoffRateVec(pos); 
        binHireRate(i) = binEmpWeight*HireRateVec(pos);     
        binQuitRate(i) = binEmpWeight*QuitRateVec(pos);     
        binSepRate(i) = binEmpWeight*SepRateVec(pos);  
    end
end
 %
 % Add mass at zero
zerospos = find( 0==EmpGrowthVec);
binEmpWeight0  = AvgEmpVec(zerospos)' / sum(AvgEmpVec(zerospos));    
binLayoffRate0 = binEmpWeight0*LayoffRateVec(zerospos);	
binHireRate0 = binEmpWeight0*HireRateVec(zerospos);     
binQuitRate0 = binEmpWeight0*QuitRateVec(zerospos);     
binSepRate0  = binEmpWeight0*SepRateVec(zerospos); 
 %
hockeyMAT    = sortrows( [[binsL;0], [binsR;0], [midpt;0], [binHireRate; binHireRate0], [binQuitRate; binQuitRate0], [binSepRate; binSepRate0], ...
                          [binLayoffRate; binLayoffRate0]], 1 );  
 %
 % 
 % Save some output for later
qtEmpGrowthVec = EmpGrowthVec;
qtAvgEmpMat = AvgEmpMat;
qtAvgEmpVec = AvgEmpVec;
qtTotAvgEmp = TotAvgEmp;

